<?php

namespace App\Models\V6;

use DB;


class AudioModel
{

    private $condition;

    public function __construct()
    {
        $today           = date_time();
        $this->condition = [
            ['isShow', '=', 1],
            ['DeleteMark', '=', 1],
            ['PublishDate', '<=', $today]
        ];
    }

    /*
     * 获取所有的音频
     * */
    public function get_audio_all()
    {
        /*每日一言*/
        $audioList = DB::table('audioday')
            ->select('AudioID', 'AudioTitle', 'FilePath1', 'ReleaseDate', 'PublishDate', "view_times")
            ->where($this->condition)
            ->orderBy('PublishDate', 'desc')->get();
        foreach ($audioList as $v)
        {
            if (strtotime($v->PublishDate) > 1000)
                $v->ReleaseDate = $v->PublishDate;
        }
        return $audioList;

    }


    /**
     * 获取音频列表
     * @param $perNumber integer 每页显示的数量
     * @param $userId    integer 用户ID
     * */
    public function get_audio_list($userId = 0)
    {
        /*每日一言*/
        $today     = date_time();
        $condition = [
            ['a.isShow', '=', 1],
            ['a.DeleteMark', '=', 1],
            ['a.PublishDate', '<=', $today]
        ];

        $db = DB::table('audioday as a');
        $colum  = [
            'a.AudioID',
            'a.AudioTitle',
            'a.FilePath1',
            'a.time_length',
            'a.ReleaseDate',
            'a.PublishDate',
            "a.view_times"
        ];

        if($userId > 0)
        {
            array_push($colum,DB::raw("IF(b.id,1,0) AS  isCollect"));
            $db->leftJoin('audio_collect as b','b.audio_id','=',DB::raw("a.AudioID and b.user_id = $userId"));
        }
        else
        {
            array_push($colum,DB::raw("0 as isCollect"));
        }

        $audioList = $db->select($colum)
            ->where($condition)
            ->orderBy('a.PublishDate', 'desc')
            ->paginate(20);

        foreach ($audioList as $v)
        {
            if (strtotime($v->PublishDate) > 1000)
            {
                $v->ReleaseDate = $v->PublishDate;
                $v->time_length = $v->time_length ."秒";
            }

        }
        return $audioList;
    }



    /**
     * 获取音频信息
     * */
    public function get_audio_info($audioId)
    {
        $audioInfo = DB::table('audioday')
            ->where($this->condition)
            ->where('AudioID', $audioId)
            ->first();
        return $audioInfo;
    }


    /**
     * 增加音频播播放次数
     * */
    public function add_audio_view_times($audioId)
    {
        return DB::table('audioday')->where('AudioID', $audioId)->increment('view_times', 1);
    }


    /**
     * 当日音频信息
     * */
    public function get_current_day_video()
    {
        $date = date('Y-m-d');
        return DB::table('audioday')
            ->where('PublishDate', $date)
            ->where('IsShow', 1)
            ->where('DeleteMark', 1)
            ->first();
    }

    /**
     * 获取最新的音频
     *
     * */
    public function get_last_vide(){

        $date = date('Y-m-d');
        return DB::table('audioday')
            ->where('PublishDate',"<=", $date)
            ->where('IsShow', 1)
            ->where('DeleteMark', 1)
            ->orderBy('AudioID','desc')
            ->first();
    }

    static function collect_video($userId,$audioId)
    {
        $collect = DB::table('audio_collect')
            ->where('user_id',$userId)
            ->where('audio_id',$audioId)
            ->first();

        if($collect)
        {
            DB::table('audio_collect')->where('id',$collect->id)->delete();
        }
        else
        {
            $data   = [
                'user_id'   => $userId,
                'audio_id'  => $audioId,
                'created_at'=> date_time()
            ];
            DB::table('audio_collect')->insert($data);
        }
    }
}